package main

import (
	"encoding/json"
	"fmt"
	"net/url"
)

func GetUrlArg(rawURL string) (string, error) {
	// 解析URL
	parsedURL, err := url.Parse(rawURL)
	if err != nil {
		fmt.Println("URL解析失败:", err)
		return "", err
	}

	// 获取查询参数
	queryParams := parsedURL.Query()
	vParam := queryParams.Get("v")

	if vParam == "" {
		return "", fmt.Errorf("未找到 'v' 参数")
	}
	return vParam, nil
}

// 清洗目录信息
func clearTocInfo(tocInfo string) (string, error) {
	var tocObject []Chapter
	err := json.Unmarshal([]byte(tocInfo), &tocObject)
	if err != nil {
		return "", err
	}
	var tocInfoStr string
	tmpLenth := len(tocObject)
	if len(tocObject) > 0 {

		for i, chapter := range tocObject {
			if (i + 1) == tmpLenth {
				tocInfoStr += string(chapter.Title)
			} else {
				tocInfoStr += string(chapter.Title) + "||"
			}
		}

		return tocInfoStr, nil
	}
	return "", nil
}
